package com.alex.senior1.week1.controller;

import java.io.File;
import java.io.IOException;
import java.util.List;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import com.alex.senior1.week1.domain.TbBrand;
import com.alex.senior1.week1.domain.TbGoods;
import com.alex.senior1.week1.domain.TbGoodskind;
import com.alex.senior1.week1.service.ITbBrandService;
import com.alex.senior1.week1.service.ITbGoodsService;
import com.alex.senior1.week1.service.ITbGoodskindService;
import com.alex.senior1.week1.vo.GoodsVo;
import com.alex.senior1.week1.vo.ResultVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

/**
 * <p>
 * 商品表 前端控制器
 * </p>
 *
 * @author alex
 * @since 2019-11-01
 */
@Controller
public class TbGoodsController {

	@Autowired
	private ITbGoodsService goodsService;
	
	@Autowired
	private ITbBrandService brandService;
	
	@Autowired
	private ITbGoodskindService goodskindService;

	@RequestMapping("/")
	public ModelAndView getGoodsList(@RequestParam(value = "name", required = false) String name, @RequestParam(value="pageNo",required=false) Integer pageNo) {
		ModelAndView view = new ModelAndView("goods-list");
		pageNo = pageNo != null ? pageNo : 1;
		Page<GoodsVo> page = new Page<GoodsVo>(pageNo, 3);
		QueryWrapper<GoodsVo> queryWrapper = new QueryWrapper<GoodsVo>();
		queryWrapper = queryWrapper.eq("tb_goods.status", true);
		if (!StringUtils.isEmpty(name)) {
			queryWrapper = queryWrapper.like("tb_goods.name", name);
		}
		IPage<GoodsVo> goodsList = goodsService.getGoodsByPageAndName(page, queryWrapper);
		view.addObject("goodsList", goodsList.getRecords());
		view.addObject("currentPage", goodsList.getCurrent());
		view.addObject("pages", goodsList.getPages());
		return view;
	}
	@RequestMapping("/toAdd")
	public ModelAndView showAddPage(){
		ModelAndView view = new ModelAndView("goods-add");
		GoodsVo goodsVo = new GoodsVo();
		List<TbBrand> brands = brandService.list();
		List<TbGoodskind> kinds = goodskindService.list();
		view.addObject("brands",brands);
		view.addObject("kinds",kinds);
		view.addObject("goods",goodsVo);
		return view;
		
	}
	/**
	 * 
	 * @Title: removeMovie
	 * @Description: 批量删除，单条删除
	 * @param id
	 * @return
	 * @return ResultVo 返回结果集：状态status和信息提示message
	 */
	@RequestMapping("/remove")
	@ResponseBody
	public ResultVo removeGoods(@RequestParam("id") List<Integer> ids) {
		ResultVo resultVo = new ResultVo();
		if (CollectionUtils.isEmpty(ids)) {
			resultVo.setStatus(0);
			resultVo.setMessage("参数传值为空！");
		}
		boolean ret = false;
		for (Integer id : ids) {
			TbGoods goods = goodsService.getById(id);
			goods.setStatus(false);
			ret = goodsService.updateById(goods);
		}
		resultVo.setStatus(ret == true ? 1 : 0);
		resultVo.setMessage(ret == true ? "删除成功！" : "删除失败！");
		return resultVo;
	}

	/**
	 * 
	 * @Title: addMovie
	 * @Description: 添加接口
	 * @param movieStr
	 * @return
	 * @return ResultVo 添加的结果集
	 */
	@RequestMapping(value = "/edit", method = RequestMethod.POST)
	@ResponseBody
	public ResultVo addGoods(TbGoods goodStr,MultipartFile file,HttpSession session) {
		ResultVo resultVo = new ResultVo();
		if(!file.isEmpty()){
			String fileName = file.getOriginalFilename();
			String filePath = session.getServletContext().getRealPath("/")+"/upload/";
			File file2 = new File(filePath);
			if(!file2.exists()){
				file2.mkdir();
			}
			try {
				file.transferTo(new File(filePath+fileName));
			} catch (IllegalStateException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			goodStr.setImgUrl("/upload/"+fileName);
		}
		if(goodStr.getId() == null){
			goodStr.setStatus(true);
		boolean ret = goodsService.save(goodStr);
		// 返回结果
		resultVo.setStatus(ret == true ? 1 : 0);
		resultVo.setMessage(ret == true ? "保存成功" : "保存失败");
		}else{
			boolean ret = goodsService.updateById(goodStr);
			// 返回结果
			resultVo.setStatus(ret == true ? 1 : 0);
			resultVo.setMessage(ret == true ? "修改成功" : "修改失败");
		}
		return resultVo;
	}

	/**
	 * 
	 * @Title: showUpdatePage
	 * @Description: 跳转修改页面
	 * @param id
	 * @return
	 * @return ModelAndView
	 */
	@RequestMapping("/toUpdate")
	public ModelAndView showUpdatePage(@RequestParam("id") Integer id) {
		// 指定修改jsp页面
		ModelAndView view = new ModelAndView("goods-add");
		GoodsVo goods = goodsService.getGoodsById(new QueryWrapper<GoodsVo>().eq("tb_goods.id", id));
		// 返回结果到页面
		List<TbBrand> brands = brandService.list();
		List<TbGoodskind> kinds = goodskindService.list();
		view.addObject("brands",brands);
		view.addObject("kinds",kinds);
		view.addObject("goods",goods);
		return view;
	}

	
	/**
	 * 
	 * @Title: showUpdatePage
	 * @Description: 跳转查看页面
	 * @param id
	 * @return
	 * @return ModelAndView
	 */
	@RequestMapping("/toShow")
	public ModelAndView showPage(@RequestParam("id") Integer id) {
		// 指定修改jsp页面
		ModelAndView mav = new ModelAndView("goods-show");
		GoodsVo goods = goodsService.getGoodsById(new QueryWrapper<GoodsVo>().eq("tb_goods.id", id));
		// 返回结果到页面
		mav.addObject("goods", goods);
		return mav;
	}
}
